﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
using RookieLibSharp.Database;
using System.Net;
using System.Net.Mail;
using RookieLibSharp.Configuration;
namespace BirthdayRemainder.WinService
{
    public class BRService
    {
        private Thread ThBack;
        private bool bStop = false;
        private bool bRegSucess = false;
        public BRService()
        {
            Funcs.InitPubVal();
            bRegSucess = Cfg.ReadBool(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_REGISTER, false);
        }
        public void StartService()
        {
            bStop = false;
            if (null == ThBack)
            {
                ThBack = new Thread(BackProc);
            }
            ThBack.Start();
        }
        public void StopService()
        {
            bStop = true;
        }

        /// <summary>
        /// 后台程序
        /// </summary>
        public void BackProc()
        {
            int nCount = 0;
            int nInterval = GetRemindInterval();
            while (true)
            {
                nCount++;
                Thread.Sleep(1000);
                if (nCount >= nInterval)
                {
                    CheckRemainder();
                    nCount = 0;
                }
                if (bStop)
                {
                    break;
                }
            }
        }
        /// <summary>
        /// 返回提醒的时间间隔，ms
        /// </summary>
        /// <returns></returns>
        private int GetRemindInterval()
        {
            int nInterval = Cfg.ReadInt(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_REMAIND_INTERVAL);//分钟数
            if ((nInterval == 0) || (nInterval > 60))
            {
                nInterval = 5;//小于等于0或者是大于60分钟按照5分钟计算
            }
            nInterval *= 60; 
#if DEBUG
            return 10;
#else
            return nInterval;
#endif
            
        }

        /// <summary>
        /// 检测是否需要提醒
        /// 如有提醒则发送消息给主窗体
        /// </summary>
        private void CheckRemainder()
        {
            if (!DbMgr.DatabaseFileExsit())
            {
                return;
            }
            List<PersonInfo> LstInfo = SftMgr.DbGetAllInfo();
            int remaindDays = Cfg.ReadInt(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_REMAIND_DAYS); ;
            foreach (var info in LstInfo)
            {
                if (info.DayToBirthday <= remaindDays)
                {
                    if (Funcs.IsANewDay())
                    {
                        //新的第一天，无条件提醒
                        ShowRemainder();
                        MailRemind();
                    }
                    else
                    {
                        bool bNotRemaind = Cfg.ReadBool(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_NOT_FRM_REMAIND);
                        if (!bNotRemaind)
                        {
                            ShowRemainder();
                        }
                        bNotRemaind = Cfg.ReadBool(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_NOT_MAIL_REMIND);
                        if (!bNotRemaind)
                        {
                            MailRemind();
                            
                        }
                    }
                    return;
                }
            }
        }
        /// <summary>
        /// 显示提醒窗口
        /// </summary>
        private void ShowRemainder()
        {
            bool bFrmRemind = Cfg.ReadBool(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_FRM_REMIND, true);
            if (bFrmRemind)
            {
                using (FrmRemainder frm = new FrmRemainder())
                {
                    frm.ShowDialog();
                }
            }
        }

        private bool MailRemind()
        {
            bool bNotRemind = Cfg.ReadBool(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_MAIL_REMIND,false);
            if (!bNotRemind)
            {
                return false;//没有勾选邮件提醒，则不提醒
            }
#if DEBUG
            Cfg.WriteConfig(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_NOT_MAIL_REMIND, true);
            return true;
#else
            MailClient mailClient = new MailClient();
            mailClient.MailBody = Funcs.GetRemindStr();
            mailClient.SendMailAddr = Cfg.ReadString(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_MAIL_REMIND_SEND_MAIL_ADDR);
            mailClient.SendMainPSD = Cfg.ReadString(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_MAIL_REMIND_SEND_MAIL_PSD);
            mailClient.ReceiveMailAddr = Cfg.ReadString(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_MAIL_REMIND_RECEIVE_MAIL_ADDR);
            mailClient.MailSubject = "[生日提醒]" + DateTime.Today.ToString("yyyy-MM-dd");
            if (mailClient.SendMail())
            {
                Cfg.WriteConfig(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_NOT_MAIL_REMIND, true);
                return true;
            }
            return false;
#endif

        }
    }
}
